package com.myspring.web.exceptions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.myspring.web.exceptions.exModel.AjaxBusinessException;
import com.myspring.web.exceptions.exModel.BusinessException;
import com.myspring.web.exceptions.exModel.MutiException;
import com.myspring.web.exceptions.exModel.NestedException;

/**
 * AOP异常处理应用
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	private Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	/**
	 * 方法参数里面不能用Model
	 * 不然会有以下异常：
	 * No suitable resolver for argument [1] [type=org.springframework.ui.Model]
	 * @param e
	 * @return
	 */
	@ExceptionHandler
	public ModelAndView handleSuperException(Exception e) {
		log.error("super异常测试", e);
		//查看异常类型
		System.out.println("====================" + e);
		ModelAndView view = new ModelAndView("/exception/super");
		view.addObject("msg", e.getMessage());
		return view;
	}
	
	@ExceptionHandler
	public String handleBusinessException(BusinessException ex) {
		log.error("业务异常", ex);
		return "/exception/business";
	}
	
	@ExceptionHandler
	public String handleNestedException(NestedException ex) {
		log.error("内嵌businessexception处理", ex);
		return "/exception/nested";
	}
	
	@ExceptionHandler
	public String handleMutiException(MutiException ex) {
		log.error("内嵌MutiException处理", ex);
		return "/exception/muti";
	}
	
	@ExceptionHandler
	@ResponseBody
	public ExceptionMessage handleAjaxException(AjaxBusinessException ex) {
		ExceptionMessage em = new ExceptionMessage();
		em.setMessage("ajax异常处理" + ex.getMessage());
		return em;
	}
}
